<?php
set_include_path($_SERVER['DOCUMENT_ROOT']);
include_once 'admin/includes/functions/admin-functions.php';
ini_set('display_errors',1);
ini_set('error_reporting',E_ALL ^ E_NOTICE ^ E_WARNING);
global  $CONFIG,$SMARTY,$DBobject;

echo "=> Cron started<br>";
$classTermObj = new ClassTermClass();
$enrol_obj = new Enrolment();

$devAdmin = "readysteadygokids@bccgmail.com";
$from = (string) $CONFIG->company->name;
$fromEmail = 'noreply@'. str_replace('www.', '', $GLOBALS['HTTP_HOST']);
$COMP = json_encode($CONFIG->company);
$SMARTY->assign('COMPANY', json_decode($COMP,TRUE));
$SMARTY->assign('DOMAIN', "http://" . $GLOBALS['HTTP_HOST']);

$events = array();
$membersEmailArr = array();
$membersPhoneArr = array();

$subject = "澳睿跑 (Ready Steady Go Kids) | 付款提醒";
$day = date('Y-m-d', strtotime('-7 days'));

//Only at THEM
if($_SERVER['REMOTE_ADDR']=='150.101.230.130' && !empty($_REQUEST['date'])){
	$day = date('Y-m-d', strtotime($_REQUEST['date']));
}

echo "=> Payment Reminder:  $day  <br>";


//Get "Payment reminder" message
$sql = "SELECT email_additional_content FROM tbl_email_additional WHERE email_additional_deleted IS NULL AND email_additional_id = '13' "; 
$res = $DBobject->wrappedSql($sql);
$email_content = unclean($res[0]['email_additional_content']);

//SMS REMOVED - REQUESTED BY CLIENT ON 20-06-2015
/* //Get "SMS - Payment reminder" message
$sql = "SELECT email_additional_content FROM tbl_email_additional WHERE email_additional_deleted IS NULL AND email_additional_id = '18' ";
$res = $DBobject->wrappedSql($sql);
$sms_content = unclean($res[0]['email_additional_content']); */

//if(empty($email_content) || empty($sms_content)) sendMail($devAdmin, $from, $fromEmail, "CRON JOB ERROR", "Content message in '$subject' is null.");
if(empty($email_content)) $events['error'][] = "Content message in '$subject' is null. ";

try {
	// Get current terms
	$sql = "SELECT user_id, user_gname, user_surname, user_email
		FROM tbl_payment 
			LEFT JOIN tbl_user ON user_id = payment_user_id 
		WHERE user_deleted IS NULL AND payment_deleted IS NULL AND payment_status = 'P' AND payment_total > 0 AND DATE(payment_created) = :payment_created
		GROUP BY user_email";
	$params = array (":payment_created"=>$day);
	if($payments = $DBobject->wrappedSql($sql, $params)){
		foreach($payments as $e){
			// FOREACH MEMBER/PARENT - EMAIL
			if(!in_array($e['user_email'], $membersEmailArr)){
				$membersEmailArr[] = $e['user_email'];
				$SMARTY->assign('data', $e);
				$SMARTY->assign('content', $email_content);
				$body = $SMARTY->fetch('cron/email-custom.tpl');
				createBulkMail(array($e['user_email']), $from, $fromEmail, $subject, $body, 0, array($e['user_id']));
				
				//SMS REMOVED - REQUESTED BY CLIENT ON 20-06-2015
				/* // FOREACH MEMBER/PARENT - SMS
				$sql = "SELECT address_telephone FROM tbl_address WHERE address_deleted IS NULL AND address_user_id = :id ORDER BY address_modified DESC LIMIT 1" ;
				if($address = $DBobject->wrappedSql($sql, array(':id' => $e['user_id']))){
					$temp = preg_replace('/\+|\s|\./', '', $address[0]['address_telephone']);
					$phone = preg_replace('/^04/','614', $temp,1);
					if(!in_array($phone, $membersPhoneArr)){
						if(preg_match("/^614/",$phone)){
							$membersPhoneArr[$e['user_id']] =  $phone;
						}
					}
				} */
			}
		}
	} 
	//SMS REMOVED - REQUESTED BY CLIENT ON 20-06-2015
	/* try {
		sendSMS($membersPhoneArr, $sms_content);
	} catch (Exception $e) {
		$events['error'][] = "Error while running sendSMS: ". $e;
	} */
	$events['records']['email'] = $membersEmailArr;
	//$events['records']['sms'] = $membersPhoneArr;
	//echo "<br>=> Emails sent: ". sendBulkMail(); // "cron_email_queue.php" will send them
		
	
} catch (Exception $e) {
	$events['error'][] = "Error while running".$e;
}

//sendMail($devAdmin, $from, $fromEmail, "CRON JOB - PAYMENT REMINDER $day", json_encode($events));
createBulkMail(array($devAdmin), $from, $fromEmail, "CRON JOB - PAYMENT REMINDER $day", json_encode($events));
echo "<br>=> Cron ended";

